package com.hg.library.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;

/**
 * Created by admin on 2017/3/1.
 */

public class SortButton extends Button {
    private static final String TAG = "SortButton";

    private float padding = 8;
    private boolean isStart;
    Paint mPaint = new Paint();

    float Y;
    int count;

    public SortButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        setBackground(null);
        mPaint.setStrokeWidth(4);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setColor(Color.argb(255, 65, 72, 75));

        mPaint.setStyle(Paint.Style.STROKE);

        if (isStart) {
            if (count % 2 == 0){
                Y += 0.4f;
            }else {
                Y -=0.4f;
            }

            if (Y >= padding - 2 || Y <= 0) {
                count ++;
                if (count == 4){
                    isStart = false;
                    count = 0;
                    Y = 0;
                }
            }
            invalidate();
        }

        {
            int layerId = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.CLIP_SAVE_FLAG);

            canvas.translate(0, Y);

            Path path = new Path();
            path.moveTo(padding * 2, padding);
            path.lineTo(padding * 2, getHeight() - padding);
            canvas.drawPath(path, mPaint);

            path.reset();
            path.moveTo(0, getHeight() - padding * 3);
            path.lineTo(padding * 2, getHeight() - padding);
            path.lineTo(padding * 4, getHeight() - padding * 3);
            canvas.drawPath(path, mPaint);


            canvas.restoreToCount(layerId);
        }

        {
            int layerId = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);

            canvas.translate(0, -Y);

            Path path = new Path();
            path.reset();
            path.moveTo(getWidth() - padding * 2, padding);
            path.lineTo(getWidth() - padding * 2, getHeight() - padding);
            canvas.drawPath(path, mPaint);

            path.reset();
            path.moveTo(getWidth() - padding * 4, padding * 3);
            path.lineTo(getWidth() - padding * 2, padding);
            path.lineTo(getWidth(), padding * 3);
            canvas.drawPath(path, mPaint);

            canvas.restoreToCount(layerId);
        }

    }

    public void click() {
        if (!isStart) {
            isStart = true;
            invalidate();
        }
    }
}
